var React = require('react');
var Layout = require('../../controls/Layout');
var ListView = require('../../controls/ListView');
var DBService = require('./DBService');
module.exports = React.createClass({
	getInitialState: function () {
		return {
			fields: [],
			view: null,
			tabItems: [
				{ text: '表数据', view: './TableData' },
				{ text: '表结构', view: './TableStruct' }
			],
			formItems: [
				{ title: 'Name', name:'Name', type:'text' },
				{ title: 'UserName', name:'UserName', type:'date' },
				{ title: 'Tenant', name:'Tenant', type:'text' },
				{ title: 'Type', name:'Type', type:'select', items: [{text:'1',value:'1'},{text:'2',value:'2'}] },
				{ title: 'File', name:'file', type:'file' },
				{ title: 'Description', name:'Description', type:'textarea' }
			]
		}
	},
	componentDidMount:function(){
		console.log(this);
	},
	componentDidUpdate: function (){
		if(this.props.table){
			var _self = this;
			DBService.getTableDesc(this.props.db, this.props.table).then(function (data){
				var _data = [];
				for(var key in data.rows){
					_data.push(data.rows[key]);
				}
				console.log(_data);
				//_self.setState({ fields: _data });
			});
		}
	},
	render:function(){
		var View = this.state.view&&require(this.state.view);
		return (
			<Layout
				begin={35}
				bStyle={{borderTop: '1px solid #3FA9F5'}}
				direction="V">
				<ListView
					skin="tab"
					fireIndex={0}
					onClick={(item)=>this.setState({ view: item.view })}
					itemRender={(item)=>{ return <span>{item.text}</span>;}}
					data={this.state.tabItems} />
				{View && <View fields={this.state.fields} db={this.props.db} table={this.props.table}/>}
			</Layout>
		);
	}
});
